﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Expo.Training.BRule;
using System.Data;
using Expo.Training.DataAccess.Audit;
using System.Data.SqlClient;
using Expo.Training.BRule.Group;
using Expo.Training.Entity;
using Expo.Training.DataAccess.Vip;

public partial class XB_Index_Default : Pagebase
{

    protected string imgHeadUrl = string.Empty;
    protected string userUrl = string.Empty;
    protected int userId = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = Expo.Training.DataAccess.Member.Member.LoadMemberInfoByMemberID(this.MemberID);
        if (dt != null && dt.Rows.Count != 0)
        {
            InitCtrl(dt.Rows[0]);
        }
        CheckIsAdmin();

    }

    private void InitCtrl(DataRow dr)
    {
        this.litNickName.Text = dr["NickName"].ToString();
        this.litScore.Text = PublicFunction.FmtNumToStr(dr["Score"]);
        //this.litStudyMoney.Text = PublicFunction.FmtNumToStr(dr["SudyMoney"]);
        this.litStudyTime.Text = PublicFunction.FmtNumToStr(dr["StudyTime"]);
        
        string loginCount = CookieManager.ReadCookie("LoginCount");
        if (String.IsNullOrEmpty(loginCount))
        {
            loginCount = "1";
        }
        this.litLoginCount.Text = loginCount;

        string lastactivedate = CookieManager.ReadCookie("LastLoginTime");
        if (String.IsNullOrEmpty(lastactivedate))
        {
            HttpHelper helper = HttpHelper.Instance;

            int count = 0;
            DateTime date = DateTime.Parse("1900-1-1");
            DataTable dt = AuditManager.SelectMemberAuditLog(2, 1, out count, 2, date, date, String.Empty, helper.UserAccount, String.Empty, "登陆系统", helper.CustomerID);
            if (dt != null && dt.Rows.Count > 0)
            {
                lastactivedate = DateTime.Parse(dt.Rows[0]["AuditDate"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            }
            CookieManager.WriteCookie("LastLoginTime", lastactivedate, DateTime.Parse("1900-1-1"));
        }
        this.litLastLoginDate.Text = DateTime.Parse(lastactivedate).ToShortDateString();
        imgHeadUrl = Base.GetMemberPhoto(dr["HeaderUrl"].ToString());

        SqlDataReader reader = GroupBLL.GetUsersInfo(this.Member.MemberID);
        if (reader.Read())
        {
            userId=Convert.ToInt32(reader["id"]);
            userUrl = reader["Url"].ToString();
        }

        //绑定VIP等级
        VipEntity _vip = new VipEntity();
        string _vipTitle;

        MemberCard_Info mci = new MemberCard_Info();
        mci.MemberID = this.MemberID;
        if (mci.Exist())
        {
            _vip = VipModule.GetVipByMemberID(this.MemberID);
            //_vipTitle = _vip.CategoryName;
            _vipTitle = "二级VIP";

            if (_vip.Category == 2)
            {
                _vipTitle += "(" + VipModule.GetLevel(int.Parse(dr["Score"].ToString())).Title + ")";
            }
        }
        else
        {
            _vipTitle = "普通会员";
        }

        this.txtVip.Text = _vipTitle;
    }

    protected void CheckIsAdmin()
    {
        Member_Group objG = new Member_Group();
        objG.MemberID = this.Member.MemberID;

        lnkAdmin.Visible = objG.Exist();
    }
}
